package dao;

import bean.DeviceInfo;
import util.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;

public class DeviceInfoDaoImpl implements DeviceInfoDao{

    public List<DeviceInfo> getAllDeviceInfoByCondition(DeviceInfo deviceInfo)
    {
        List<DeviceInfo> deviceInfos = new LinkedList<>();
        try {
            Connection connection = null;
            ResultSet rs = null;
            PreparedStatement ps = null;
            connection = JdbcUtils.getConnection();
            String sql = "select * from t_device_info where 1=1";
            if (deviceInfo.getTrainNumber() != "null") {
                sql += " and TrainNumber=\"" + deviceInfo.getTrainNumber()+"\"";
            }
            if (deviceInfo.getVehicleNumber() != -1) {
                sql += " and VehicleNumber=" + deviceInfo.getVehicleNumber();
            }
            if (deviceInfo.getBogie() != -1)
            {
                sql += " and Bogie="+deviceInfo.getBogie();
            }
            if (deviceInfo.getVersion() != "null") {
                sql += " and Version=\"" + deviceInfo.getVersion()+"\"";
            }
            if(deviceInfo.getStatusOfDevice()!=0)
            {
                sql += " and StatusOfDevice="+deviceInfo.getStatusOfDevice();
            }
            System.out.println(sql);
            ps = connection.prepareStatement(sql);
            rs = ps.executeQuery();


            while(rs.next())
            {
                DeviceInfo deviceInfo1 = new DeviceInfo();
                deviceInfo1.setTrainNumber(rs.getString(1));
                deviceInfo1.setVehicleNumber(rs.getInt(2));
                deviceInfo1.setBogie(rs.getInt(3));
                deviceInfo1.setVersion(rs.getString(4));
                deviceInfo1.setStatusOfDevice(rs.getInt(5));
                deviceInfos.add(deviceInfo1);
            }

        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return deviceInfos;
    }
}
